﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;

namespace EnglishWordsAnalyster
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //Match();
            List<string> insertSqls = PrepareInsertSqls();

            ExcSqls(insertSqls);
        }

        private void ExcSqls(List<string> insertSqls)
        {


            if (BaseEntitys.ExcuteSQL(insertSqls))
            {
                MessageBox.Show("OK");
                return;
            }

            MessageBox.Show("fail");
        }


        private List<string> PrepareInsertSqls()
        {
             DataTable wordsDT = BaseEntitys.QueryToDataTable("select * from words");

            List<string> insertSqls = new List<string>();

            foreach (DataRow dr in wordsDT.Rows)
            {
                List<string> insertSql = GetInsertSql(dr);

                insertSqls.AddRange(insertSql);
            }

            return insertSqls;
        }

        private List<string> GetInsertSql(DataRow dr)
        {
            WordInfo wdInfo = GetWordInfo(dr);

            List<string> sqls = new List<string>();

            foreach (ClassInfo item in wdInfo.Meaning)
            {
                sqls.Add(string.Format("Insert into details (word,class,definition) values('{0}','{1}','{2}')", wdInfo.Word, item.Class,item.Definition));
            }

            return sqls;


        }

        private WordInfo GetWordInfo(DataRow dr)
        {
             string word = dr["words"].ToString();

            string meaning=dr["meaning"].ToString();

            WordInfo wInfo = new WordInfo(word,meaning);
            wInfo.SetMeaning(meaning);
            return wInfo;
        }

        private void Match()
        {
            MatchCollection mc;

            String[] results = new String[20];

            int[] matchposition = new int[20];

            Regex r = new Regex(@"[a-z,A-Z]+\."); //定义一个Regex对象实例

            mc = r.Matches("a. 上面的<br>ad. 在上面<br>n. 上面<br>prep. 在...上面");

            for (int i = 0; i < mc.Count; i++) //在输入字符串中找到所有匹配
            {
                results[i] = mc[i].Value; //将匹配的字符串添在字符串数组中
                matchposition[i] = mc[i].Index; //记录匹配字符的位置
            }

            var ii = 1;
        }
        
    }
}
